//
//  ForgetMMViewController.m
//  ShouYi
//
//  Created by yurui on 16/2/24.
//  Copyright © 2016年 yurui. All rights reserved.
//

#import "ForgetMMViewController.h"

@interface ForgetMMViewController ()
@property(nonatomic,strong) UITextField *phoneTF;
@property(nonatomic,strong) UITextField *mmTF;
@property(nonatomic,strong) UITextField *yzmTF;
@property(nonatomic,strong) UIButton *yzmBt;
@property(nonatomic,strong) UIButton *submitBt;

@end

@implementation ForgetMMViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"修改密码";
    [self addSubViews];
}

- (void)addSubViews{
    self.phoneTF = [[UITextField  alloc]initWithFrame:CGRectMake(15, 105, kWidth - 30, 35)];
    _phoneTF.borderStyle = UITextBorderStyleRoundedRect;
    _phoneTF.placeholder = @"手机号";
    _phoneTF.font = [UIFont systemFontOfSize:15];
    _phoneTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [kUserDefault setObject:_phoneTF.text forKey:@"phoneNum"];
    [self.view addSubview:_phoneTF];
    
    self.mmTF = [[UITextField  alloc]initWithFrame:CGRectMake(15, 160, kWidth - 30, 35)];
    _mmTF.borderStyle = UITextBorderStyleRoundedRect;
    _mmTF.placeholder = @"密码";
    _mmTF.font = [UIFont systemFontOfSize:15];
    _mmTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:_mmTF];
    
    self.yzmTF = [[UITextField  alloc]initWithFrame:CGRectMake(15, 215, kWidth / 2 , 35)];
    _yzmTF.borderStyle = UITextBorderStyleRoundedRect;
    _yzmTF.placeholder = @"验证码";
    _yzmTF.font = [UIFont systemFontOfSize:15];
    _yzmTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:_yzmTF];
    
    self.yzmBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _yzmBt.frame = CGRectMake(kWidth / 2 + 30, 215, kWidth / 2 - 45, 35);
    _yzmBt.backgroundColor = [UIColor redColor];
    [_yzmBt setTitle:@"发送验证码" forState:UIControlStateNormal];
    _yzmBt.titleLabel.font = [UIFont systemFontOfSize:14];
    [_yzmBt addTarget:self action:@selector(getYZMAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_yzmBt];
    
    self.submitBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _submitBt.frame = CGRectMake(15, 280, kWidth - 30, 30);
    _submitBt.backgroundColor = kBtColor;
    [_submitBt setTitle:@"提交" forState:UIControlStateNormal];
    _submitBt.titleLabel.font = [UIFont systemFontOfSize:14];
    [_submitBt addTarget:self action:@selector(submitAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_submitBt];

}

// 获取验证码
- (void)getYZMAction{
    if (_phoneTF.text.length == 0) {
        UIAlertView *aalertV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请输入手机号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aalertV show];
    }else{
    __block int timeout = 60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [_yzmBt setTitle:@"获取验证码" forState:UIControlStateNormal];
                _yzmBt.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //NSLog(@"____%@",strTime);
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                NSString *str = [NSString stringWithFormat:@"重新发送(%@)",strTime];
                [_yzmBt setTitle:str forState:UIControlStateNormal];
                [UIView commitAnimations];
                _yzmBt.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString * uuid = [kUserDefault objectForKey:@"CFUUID"];
    NSDictionary *parmeters = @{@"mobile":_phoneTF.text,@"uuid":uuid};
    [manager POST:@"http://wx1.01zg.com/plugins/app/chaozan/main.php?action=yanzm&sid=2216&mobile=&uuid=" parameters:parmeters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success:%@",responseObject);
        NSLog(@"_______str = %@",_phoneTF.text);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"erros: %@",error);
    }];
    }
    
}
- (void)submitAction{
#warning 判断该手机号是否已经注册过
    
    if (_mmTF.text != nil) {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        // [NSSet setWithObject:@"text/html"];
        // NSLog(@"_phoneTF.text = %@",_phoneTF.text);
        NSString * uuid = [kUserDefault objectForKey:@"CFUUID"];
        NSDictionary *parmeters = @{@"mobile":_phoneTF.text,@"userpass":_mmTF.text,@"yzm":_yzmTF.text,@"uuid":uuid};
        [manager POST:@"http://wx1.01zg.com/plugins/app/chaozan/main.php?action=userpass_modify&sid=2216&mobile=&userpass=&yzm=&uuid=" parameters:parmeters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"Success:%@",responseObject);
            NSLog(@"密码修改成功");
            NSString *errcode = responseObject[@"errcode"];
            int err = [errcode intValue];
            if (err == 0) {
                //self.delegate.isLogin = 1;
                [self.navigationController popViewControllerAnimated:YES];
            }else if (err == 21){
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"验证码输入错误" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"erros: %@",error);
        }];
        
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"两次密码输入不同" delegate:self cancelButtonTitle:@"重新输入" otherButtonTitles:nil, nil];
        self.mmTF.text = nil;
        [alert show];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
